package com.newgrand.annexserver.controller;

import com.alibaba.druid.util.StringUtils;
import com.newgrand.annexcommon.constant.DBUserTypeConstants;
import com.newgrand.annexcommon.constant.StoreStrateType;
import com.newgrand.annexcommon.model.DbConfigEnity;
import com.newgrand.annexcommon.model.Result;
import com.newgrand.annexcommon.utils.DbUtil;
import com.newgrand.annexcommon.utils.NoUtil;
import com.newgrand.annexcommon.utils.ResultUtil;
import com.newgrand.annexserver.beans.Dbconfig;
import com.newgrand.annexserver.datasource.NgDynamicDataSourceBeanRegistry;
import com.newgrand.annexserver.service.DbconfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ：minyang
 * @date ：Created in 2019/9/6 15:18
 */
@RestController
@RequestMapping("/dBConfig")
public class DBConfigController {

    @Autowired
    DbconfigService dbconfigService;

    @PostMapping("/addDBConfig")
    public Result addDBConfig(Dbconfig dbconfig) {
        if (dbconfig == null) {
            return ResultUtil.fail("dbconfig 为空");
        }
        if (StringUtils.isEmpty(dbconfig.getUrl()) || StringUtils.isEmpty(dbconfig.getDriverClassName()) || StringUtils.isEmpty(dbconfig.getUsername())
                || StringUtils.isEmpty(dbconfig.getPassword()) || dbconfig.getStoreStratetype() == null) {
            return ResultUtil.fail("Url或 DriverClassName 或 Username 或 Password 或 StoreStratetype 为空");
        }
        //测试业务数据库信息
        if (!DbUtil.checkConnection(dbconfig.getUrl(), dbconfig.getUsername(), dbconfig.getPassword())) {
            return ResultUtil.fail("业务数据库连接信息错误");
        }
        if (dbconfig.getStoreStratetype() == StoreStrateType.mysql || dbconfig.getStoreStratetype() == StoreStrateType.oracle
                || dbconfig.getStoreStratetype() == StoreStrateType.sqlserver) {
            if (StringUtils.isEmpty(dbconfig.getStoreUrl()) || StringUtils.isEmpty(dbconfig.getStoreDriverClassName()) || StringUtils.isEmpty(dbconfig.getStoreUsername())
                    || StringUtils.isEmpty(dbconfig.getStorePassword())) {
                return ResultUtil.fail("StoreUrl 或 DriverClassName 或 Username 或 Password 或 StoreStratetype 为空");
            }
            //测试文件存储数据库信息
            if (!DbUtil.checkConnection(dbconfig.getStoreUrl(), dbconfig.getStoreUsername(), dbconfig.getStorePassword())) {
                return ResultUtil.fail("文件存储数据库连接信息错误");
            }

        } else if (dbconfig.getStoreStratetype() == StoreStrateType.mongo) {
            if (StringUtils.isEmpty(dbconfig.getStoreUrl()) || StringUtils.isEmpty(dbconfig.getStorePort()) || StringUtils.isEmpty(dbconfig.getStoreUsername())
                    || StringUtils.isEmpty(dbconfig.getStorePassword())) {
                return ResultUtil.fail("StoreUrl 或 StorePort 或 Username 或 StoreUsername 或 StorePassword 为空");
            }
        }
        String dbtoken = NoUtil.getDbToken();
        dbconfigService.addDBConfig(dbtoken, dbconfig);
        //注册数据源
        DbConfigEnity enity1 = new DbConfigEnity();
        enity1.setDbtoken(dbtoken);
        enity1.setdBUserType(DBUserTypeConstants.business_type);
        enity1.setDriverClassName(dbconfig.getDriverClassName());
        enity1.setUrl(dbconfig.getUrl());
        enity1.setPassword(dbconfig.getPassword());
        enity1.setUsername(dbconfig.getUsername());
        NgDynamicDataSourceBeanRegistry.updateDynamicDataSource(enity1);
        if (dbconfig.getStoreStratetype() == StoreStrateType.mysql || dbconfig.getStoreStratetype() == StoreStrateType.oracle
                || dbconfig.getStoreStratetype() == StoreStrateType.sqlserver) {
            //注册存储数据源
            DbConfigEnity enity2 = new DbConfigEnity();
            enity2.setDbtoken(dbtoken);
            enity2.setdBUserType(DBUserTypeConstants.store_type);
            enity2.setDriverClassName(dbconfig.getStoreDriverClassName());
            enity2.setUrl(dbconfig.getStoreUrl());
            enity2.setPassword(dbconfig.getStorePassword());
            enity2.setUsername(dbconfig.getStoreUsername());
            NgDynamicDataSourceBeanRegistry.updateDynamicDataSource(enity2);
        }
        return ResultUtil.success(dbtoken);
    }


}
